// ObjMov.cpp : implementation file
//

#include "stdafx.h"
#include "VolDia.h"
#include "ObjMov.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern CVolDiaApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CObjMov dialog


CObjMov::CObjMov(CWnd* pParent /*=NULL*/)
	: CDialog(CObjMov::IDD, pParent)
{
	this->m_Modified = false;
	this->m_loggedUser = 0;
	//{{AFX_DATA_INIT(CObjMov)
	m_mastWeight = _T("");
	m_MastPhys = _T("");
	m_MastAngle = _T("");
	m_CellType = _T("");
	m_ClGrPos = _T("");
	m_FaGrPos = _T("");
	m_FaGrL = _T("");
	m_MastVirt = _T("");
	m_MastZone = _T("");
	//}}AFX_DATA_INIT
}


void CObjMov::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CObjMov)
	DDX_Control(pDX, IDC_ANG, m_AngleCtrl);
	DDX_Control(pDX, IDC_CONFIRM, m_Confirm);
	DDX_Control(pDX, IDC_LISTMAST, m_listMast);
	DDX_Control(pDX, IDC_LISTCELL, m_listCell);
	DDX_Control(pDX, IDC_CMB_ZONE, m_cmbZone);
	DDX_Control(pDX, IDC_CMB_MAST, m_cmbMast);
	DDX_Control(pDX, IDC_CMB_CELL, m_cmbCell);
	DDX_Text(pDX, IDC_MASTWEIGHT, m_mastWeight);
	DDX_Text(pDX, IDC_MAST_PHYS, m_MastPhys);
	DDX_Text(pDX, IDC_ANG, m_MastAngle);
	DDX_Text(pDX, IDC_CELL_TYPE, m_CellType);
	DDX_Text(pDX, IDC_CLGR, m_ClGrPos);
	DDX_Text(pDX, IDC_FAGR, m_FaGrPos);
	DDX_Text(pDX, IDC_FAGRL, m_FaGrL);
	DDX_Text(pDX, IDC_MAST_VIRT, m_MastVirt);
	DDX_Text(pDX, IDC_MAST_ZONE, m_MastZone);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CObjMov, CDialog)
	//{{AFX_MSG_MAP(CObjMov)
	ON_CBN_SELCHANGE(IDC_CMB_ZONE, OnSelchangeCmbZone)
	ON_CBN_SELCHANGE(IDC_CMB_CELL, OnSelchangeCmbCell)
	ON_CBN_SELCHANGE(IDC_CMB_MAST, OnSelchangeCmbMast)
	ON_BN_CLICKED(IDC_CONFIRM, OnConfirm)
	ON_BN_CLICKED(IDC_RIGHT, OnRight)
	ON_BN_CLICKED(IDC_LEFT, OnLeft)
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CObjMov message handlers

BOOL CObjMov::OnInitDialog() 
{
	CDialog::OnInitDialog();
	RETCOND		retCnd;
	FILEIDX		fileZone;
	t_zone		zone;
	int			rec;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;
	
	fileZone.num = F_ZONE;
	this->CheckUser();
	this->m_cmbZone.ResetContent();
	this->m_cmbMast.ResetContent();
	this->m_cmbCell.ResetContent();

	this->m_cmbMast.InsertString(MAIN_MAST, _T("Main Mast"));
	for(rec = 1; rec < N_MAX_ZONE; rec++){
		retCnd = FileRead(&fileZone, rec, 1, &zone, sizeof(zone));
		if(retCnd != R_NORMAL)
			return(FALSE);
		if(!_tcscmp(zone.name, _T("")))
			break;
		else if(zone.virtualZone != 0)
			continue;
		else
			this->m_cmbZone.InsertString(rec - 1, zone.name);
	}

	this->m_listCell.InsertColumn(1,	_T("Name"),		LVCFMT_LEFT, 95,		0 );
	this->m_listCell.InsertColumn(2,	_T("Type"),		LVCFMT_LEFT, 100,		0 );
	this->m_listCell.InsertColumn(3,	_T("Weight"),	LVCFMT_CENTER, 55,		0 );
	this->m_listMast.InsertColumn(1,	_T("Name"),		LVCFMT_LEFT, 95,		0 );
	this->m_listMast.InsertColumn(2,	_T("Type"),		LVCFMT_LEFT, 100,		0 );
	this->m_listMast.InsertColumn(3,	_T("Weight"),	LVCFMT_CENTER, 55,		0 );
	
	this->m_cmbZone.SetCurSel(0);
	this->m_cmbMast.SetCurSel(0);
	UpdateCmbCell();
	UpdateData(false);
	this->UpdateMastObjList(0);	

	GetTagIdx(TWN_START_MOV, &tagIdx);
	TagRead(&tagIdx, &tagVal);
	if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
		MessageBox(_T("FHM is currently in operation, \nobject movements forbidden."),  _T("Attention"), MB_OK | MB_ICONINFORMATION);
	
	this->OnRefresh();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CObjMov::UpdateCmbCell(){
	RETSTAT		retSt;
	RETCOND		retCnd;
	FILEIDX		fileCell;
	t_cell		cell;
	int			rec;
	int			startRec, stopRec;
	GENIDX		contType;
	GENIDX		zoneIdx;
	GENIDX		zoneIOIdx;
	ZONENAME	zoneName;
	TCHAR		coord[15];

	this->CheckUser();

	retSt = GetZoneIdx(ZONE_IO, &zoneIOIdx);
	this->m_cmbZone.GetLBText(this->m_cmbZone.GetCurSel(), zoneName);
	retSt = GetZoneIdx(zoneName, &zoneIdx);
	if(retSt != RT_NORMAL)
		return;

	fileCell.num = F_CE01 + zoneIdx - 1;

	this->m_cmbCell.ResetContent();
	
	if(zoneIdx == zoneIOIdx){
		retSt = GetDynCurrentCont(&contType);
		switch(contType){
		case Z1_NO_CONT:
			MessageBox(_T("No container in zone 1, select a container to execute operations in this zone."), _T("Warning"), MB_OK | MB_ICONINFORMATION);
			goto END;
			break;
		case Z1_CONT_TK13:
			startRec = 1;
			stopRec = CONT_TK13_CELLS;
			break;
		case Z1_CONT_LOAD:
			startRec = CONT_TK13_CELLS + 1;
			stopRec = CONT_TK13_CELLS + CONT_LOAD_CELLS;
			break;
		case Z1_CONT_HECA:
			startRec = CONT_TK13_CELLS + CONT_LOAD_CELLS + 1;
			stopRec = CONT_TK13_CELLS + CONT_LOAD_CELLS + CONT_HECA_CELLS;
			break;
		default:
			MessageBox(_T("No container in zone 1, select a container to execute operations in this zone."), _T("Warning"), MB_OK | MB_ICONINFORMATION);
			goto END;
			break;
		}
	}
	else{
		startRec = 1;
		stopRec = N_MAX_CELL;
	}

	for(rec = startRec; rec <= stopRec; rec++){
		retCnd = FileRead(&fileCell, rec, 1, &cell, sizeof(cell));
		if(retCnd != R_NORMAL)
			return;
		if((cell.Virt_X == 0) && (cell.Virt_Y == 0))
			break;
		else if((cell.Virt_X % 100) == 0){
			continue;
		}
		else{
			if(cell.type != CELL_CORR_VIRT){
				_stprintf_s(coord, _T("(%d,%d)"), cell.Virt_X, cell.Virt_Y);
				this->m_cmbCell.AddString(coord);
			}
		}
	}
	this->m_cmbCell.SetCurSel(0);
	this->UpdateCellObjList(0);
END:
	UpdateData(true);
}

void CObjMov::OnSelchangeCmbZone() 
{
	this->CheckUser();
	this->UpdateCmbCell();
}

void CObjMov::UpdateMastObjList(int type){
	RETSTAT		retSt;
	float		weight;
	float		totWeight;
	int			i;
	GENIDX		mastType;	
	OBJNAME		objName;
	TCHAR		objtDescr[50];
	int			objNum;

	this->CheckUser();
	objNum = 0;
	totWeight = 0.0;

	mastType = this->m_cmbMast.GetCurSel();

	if(!type){
		retSt = GetMastSituation(&this->objOnMast);
		if(retSt != RT_NORMAL)
			return;
	}

	this->m_listMast.DeleteAllItems();

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->objOnMast.obj[i] != 0){
			objNum++;
			retSt = GetObjtDescription(this->objOnMast.obj[i], objtDescr);
			if(retSt != RT_NORMAL)
				return;
			if(i < N_MAX_NAMED_OBJ){
				retSt = GetObjName(this->objOnMast.o_id[i], objName);
				if(retSt != RT_NORMAL)
					return;
				retSt = GetObjWeight(this->objOnMast.o_id[i], &weight);
				if(retSt != RT_NORMAL)
					return;
				totWeight += weight;
			}
			else{
				retSt = GetObjtName(this->objOnMast.obj[i], objName);
				if(retSt != RT_NORMAL)
					return;
				retSt = GetObjtWeight(this->objOnMast.obj[i], &weight);
				if(retSt != RT_NORMAL)
					return;
				totWeight += weight;
			}
			this->m_listMast.InsertItem(LVIF_TEXT, objNum - 1, objName, NULL, NULL, NULL, NULL);
			this->m_listMast.SetItemText(objNum - 1, 1, objtDescr);
			_stprintf_s(objtDescr, _T("%.1f Kg"), weight);
			this->m_listMast.SetItemText(objNum - 1, 2, objtDescr);
		}
	}
	this->m_mastWeight.Format(_T("%.1f Kg"), totWeight);
	UpdateData(false);
}

void CObjMov::UpdateCellObjList(int type){
	RETSTAT		retSt;
	int			vx, vy;
	TCHAR		coord[15];
	int			i;
	float		weight;
	OBJNAME		objName;
	TCHAR		objtDescr[50];
	int			objNum;
	int			cellType;

	objNum = 0;
	vx = 0;
	vy = 0;

	this->CheckUser();
	this->m_listCell.DeleteAllItems();	

	UpdateData(true);
	this->m_cmbCell.GetLBText(this->m_cmbCell.GetCurSel(), coord);
	_stscanf_s(coord, _T("(%d,%d)"), &vx, &vy);

	if(!type){
		retSt = GetCellSituation(vx, vy, &this->objInCell);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error retrieving cell situation"),  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}
	}

	retSt = GetCellType(vx, vy, &cellType);
	if(retSt != RT_NORMAL)
		MessageBox(_T("Cannot retrieve cell type."),  _T("Error"), MB_OK | MB_ICONERROR);
	else{
		switch(cellType){
		case CELL_FA:
			this->m_CellType.Format(_T("FA storage"));
			break;
		case CELL_HECA:
			this->m_CellType.Format(_T("Hermetic Cask storage"));
			break;
		case CELL_COVER_PLUG:
			this->m_CellType.Format(_T("Plug seat/Cluster viewing"));
			break;
		case CELL_COVER_PLUG_EX:
			this->m_CellType.Format(_T("Plug seat/Cluster casing"));
			break;
		case CELL_FFDS_CASK:
			this->m_CellType.Format(_T("FFDS Cask"));
			break;
		default:
			this->m_CellType.Format(_T("Unknown"));
			break;
		}
		UpdateData(false);
	}
	
	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		if(this->objInCell.obj[i] != 0){
			objNum++;
			retSt = GetObjtDescription(this->objInCell.obj[i], objtDescr);
			if(retSt != RT_NORMAL)
				return;
			if(i < N_MAX_NAMED_OBJ){
				retSt = GetObjName(this->objInCell.o_id[i], objName);
				if(retSt != RT_NORMAL)
					return;
				retSt = GetObjWeight(this->objInCell.o_id[i], &weight);
				if(retSt != RT_NORMAL)
					return;
			}
			else{
				retSt = GetObjtName(this->objInCell.obj[i], objName);
				if(retSt != RT_NORMAL)
					return;
				retSt = GetObjtWeight(this->objInCell.obj[i], &weight);
				if(retSt != RT_NORMAL)
					return;
			}
			this->m_listCell.InsertItem(LVIF_TEXT, objNum - 1, objName, NULL, NULL, NULL, NULL);
			this->m_listCell.SetItemText(objNum - 1, 1, objtDescr);
			_stprintf_s(objtDescr, _T("%.1f Kg"), weight);
			this->m_listCell.SetItemText(objNum - 1, 2, objtDescr);
		}
	}
	UpdateData(false);
}

void CObjMov::OnSelchangeCmbCell() 
{
	this->CheckUser();
	this->UpdateCellObjList(0);
}

void CObjMov::OnSelchangeCmbMast() 
{
	if(this->m_cmbCell.GetCurSel() == MAIN_MAST)
		this->m_AngleCtrl.EnableWindow(true);
	else
		this->m_AngleCtrl.EnableWindow(false);

	this->CheckUser();
	this->UpdateMastObjList(0);	
	this->UpdateCellObjList(0);
	this->OnRefresh();
}

void CObjMov::OnConfirm() 
{
	RETSTAT		retSt;
	int			vx, vy;
	TCHAR		coord[15];
	bool		retB;
	
	this->CheckUser();
	if(this->m_Modified){
		/*
		if(this->m_MastOnCell == false){
			MessageBox("Mast is not currently on a cell, move mast to a valid cell.",  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}
		*/
		retSt = UpdateMastSituation(&this->objOnMast);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error updating mast situation"),  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}

		this->m_cmbCell.GetLBText(this->m_cmbCell.GetCurSel(), coord);
		_stscanf_s(coord, _T("(%d,%d)"), &vx, &vy);
		retSt = UpdateCellSituation(vx, vy, &this->objInCell);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error updating cell situation"),  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}
	
		retB = SetObjectOnMast((GENIDX)(this->m_cmbMast.GetCurSel()), this->objOnMast);
		if(!retB){
			MessageBox(_T("Error updating objects on mast tags."),  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}
		
		retSt = SetWeightOnMast(this->objOnMast);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Error setting expected weight on mast"),  _T("Error"), MB_ICONERROR | MB_OK);
			return;
		}

		if(theApp.m_pDlg->m_pManual != NULL){
			theApp.m_pDlg->m_pManual->SendMessage(WM_CHNG_MAST, 0, 0);
			theApp.m_pDlg->m_pManual->SendMessage(WM_CHNG_CELL, vx, vy);
		}

		SetDynSPRefresh(1);

		MessageBox(_T("Objects movement successfully completed."), _T("Success"), MB_ICONINFORMATION | MB_OK);
		
		this->m_Modified = false;
	}
}

void CObjMov::OnRight() 
{
	RETSTAT			retSt;
	int				count;
	int				numSelected;
	int				objtPos;
	int				nextItem;
	int				vx, vy;
	int				mastVx, mastVy;
	POSITION		pos;
	OBJNAME			objName;
	GENIDX			objtIdx;
	GENIDX			objIdx;
	TCHAR			objtDescr[MAX_OBJ_DESCR];
	TCHAR			coord[15];
	TCHAR			mastCoord[15];
//	char			msgBox[256];
//	int				mastOnObj;
//	int				latchStatus;

	count = 0;

	this->CheckUser();
/*
	if(this->m_MastOnCell == false){
		MessageBox("Mast is not currently on a cell, move mast to a valid cell.",  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
*/	
	this->m_cmbCell.GetLBText(this->m_cmbCell.GetCurSel(), coord);
	_stscanf_s(coord, _T("(%d,%d)"), &vx, &vy);
	_stprintf_s(mastCoord, _T("%s"), this->m_MastVirt);
	_stscanf_s(mastCoord, _T("(%d,%d)"), &mastVx, &mastVy);
	
	/*
	if((mastVx != vx) || (mastVy != vy)){
		_stprintf_s(msgBox, "Mast is not currently on cell (%d, %d), cannot move object.", vx, vy);
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}

	retSt = GetMastOnObject(this->m_cmbMast.GetCurSel(), &mastOnObj);
	if(retSt != RT_NORMAL){
		_stprintf_s(msgBox, "Error checking if mast is on object.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
	if(!mastOnObj){
		_stprintf_s(msgBox, "Selected mast is out of the expected quote to insert objects in this zone.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}

	retSt = GetLatchStatus(this->m_cmbMast.GetCurSel(), &latchStatus);
	if(retSt != RT_NORMAL){
		_stprintf_s(msgBox, "Error checking mast FA gripper latch status.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
	if(!latchStatus){
		_stprintf_s(msgBox, "FA gripper latch must be opened to move object from mast to cell.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}

	*/
	pos = this->m_listMast.GetFirstSelectedItemPosition();
	if(pos == NULL)
		return;

	numSelected = this->m_listMast.GetSelectedCount();
	
	while(count < numSelected){
		if(count)
			nextItem = this->m_listMast.GetNextSelectedItem(pos);
		else
			nextItem = (int)pos;
		this->m_listMast.GetItemText(nextItem - 1, 1, objtDescr, MAX_OBJ_DESCR);
		retSt = GetObjtIdxFromDescription(objtDescr, &objtIdx);
		retSt = GetObjtGenericTypeCode(objtIdx, &objtPos);
		objIdx = 0;
		if(objtPos < N_MAX_NAMED_OBJ){
			this->m_listMast.GetItemText(nextItem - 1, 0, objName, sizeof(OBJNAME));
			retSt = GetObjIdx(objName, &objIdx);
		}
		retSt = InsertObjectEx(vx, vy, &this->objInCell, &this->objOnMast, objIdx, objtIdx);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Object movement not allowed."), _T("Warning"), MB_OK | MB_ICONWARNING);
			break;
		}
		count++;
	}
	this->m_Modified = true;
	this->UpdateCellObjList(1);
	this->UpdateMastObjList(1);
	UpdateData(false);
}

void CObjMov::OnLeft() 
{
	RETSTAT			retSt;
	int				count;
	int				numSelected;
	int				objtPos;
	int				nextItem;
	POSITION		pos;
	OBJNAME			objName;
	GENIDX			objtIdx;
	GENIDX			objIdx;
	TCHAR			objtDescr[MAX_OBJ_DESCR];
	int				mastVx, mastVy, vx, vy;
	TCHAR			coord[15];
	TCHAR			mastCoord[15];

	count = 0;

	this->CheckUser();
/*	
	if(this->m_MastOnCell == false){
		MessageBox("Mast is not currently on a cell, move mast to a valid cell.",  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
*/
	this->m_cmbCell.GetLBText(this->m_cmbCell.GetCurSel(), coord);
	_stscanf_s(coord, _T("(%d,%d)"), &vx, &vy);
	_stprintf_s(mastCoord, _T("%s"), this->m_MastVirt);
	_stscanf_s(mastCoord, _T("(%d,%d)"), &mastVx, &mastVy);
	
	/*
	if((mastVx != vx) || (mastVy != vy)){
		_stprintf_s(msgBox, "Mast is not currently on cell (%d, %d), cannot move object.", vx, vy);
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}

	retSt = GetMastOnObject(this->m_cmbMast.GetCurSel(), &mastOnObj);
	if(retSt != RT_NORMAL){
		_stprintf_s(msgBox, "Error checking if mast is on object.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
	if(!mastOnObj){
		_stprintf_s(msgBox, "Selected mast is out of the expected quote to remove objects from this zone.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}

	retSt = GetLatchStatus(this->m_cmbMast.GetCurSel(), &latchStatus);
	if(retSt != RT_NORMAL){
		_stprintf_s(msgBox, "Error checking mast FA gripper latch status.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
	if(latchStatus){
		_stprintf_s(msgBox, "FA gripper latch must be closed to move object from cell to mast.");
		MessageBox(msgBox,  _T("Error"), MB_ICONERROR | MB_OK);
		return;
	}
	
	  */
	pos = this->m_listCell.GetFirstSelectedItemPosition();
	if(pos == NULL)
		return;
	numSelected = this->m_listCell.GetSelectedCount();
	
	while(count < numSelected){
		if(count)
			nextItem = this->m_listCell.GetNextSelectedItem(pos);
		else
			nextItem = (int)pos;
		this->m_listCell.GetItemText(nextItem - 1, 1, objtDescr, MAX_OBJ_DESCR);
		retSt = GetObjtIdxFromDescription(objtDescr, &objtIdx);
		retSt = GetObjtGenericTypeCode(objtIdx, &objtPos);
		objIdx = 0;
		if(objtPos < N_MAX_NAMED_OBJ){
			this->m_listCell.GetItemText(nextItem - 1, 0, objName, sizeof(OBJNAME));
			retSt = GetObjIdx(objName, &objIdx);
		}
		retSt = RemoveObject(&this->objInCell, &this->objOnMast, objIdx, objtIdx);
		if(retSt != RT_NORMAL){
			MessageBox(_T("Object movement not allowed."), _T("Warning"), MB_OK | MB_ICONWARNING);
			break;
		}
		count++;
	}
	this->m_Modified = true;
	this->UpdateCellObjList(1);
	this->UpdateMastObjList(1);
	UpdateData(false);
}

void CObjMov::OnExit() 
{
	int		risp;

	if(this->m_Modified){
		risp = MessageBox(_T("System detected changes not confirmed, confirm them before exit?"), _T("Attention!"), MB_YESNOCANCEL | MB_ICONINFORMATION);
		switch(risp){
		case IDYES:
			this->OnConfirm();
			break;
		case IDCANCEL:
			return;
			break;
		}
	}
	CDialog::OnCancel();	
}


bool CObjMov::SetControls(void){
	RETSTAT		retSt;
	
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	switch(this->m_loggedUser){
	case SYSMGR:
	case LEVEL1:
		this->m_cmbCell.EnableWindow(true);
		this->m_cmbMast.EnableWindow(true);
		this->m_cmbZone.EnableWindow(true);
		this->m_Confirm.EnableWindow(true);
		break;
	case LEVEL2:
		this->m_cmbCell.EnableWindow(true);
		this->m_cmbMast.EnableWindow(true);
		this->m_cmbZone.EnableWindow(true);
		this->m_Confirm.EnableWindow(true);
		break;
	case LEVEL3:
		this->m_cmbCell.EnableWindow(true);
		this->m_cmbMast.EnableWindow(true);
		this->m_cmbZone.EnableWindow(true);
		this->m_Confirm.EnableWindow(true);
		break;
	case LEVEL4:
		this->m_cmbCell.EnableWindow(false);
		this->m_cmbMast.EnableWindow(false);
		this->m_cmbZone.EnableWindow(false);
		this->m_Confirm.EnableWindow(false);
		break;
	case LEVEL5:
		this->m_cmbCell.EnableWindow(false);
		this->m_cmbMast.EnableWindow(false);
		this->m_cmbZone.EnableWindow(false);
		this->m_Confirm.EnableWindow(false);
		break;
	}
	return true;
}

bool CObjMov::CheckUser(void){
	RETSTAT		retSt;
	int			currentUser;
	
	currentUser = this->m_loggedUser;
	//Controllo l'utente//
	retSt = GetLoggedUserLevel(theApp.m_pDlg->m_opStation, &this->m_loggedUser);
	if(retSt != RT_NORMAL){
		MessageBox(_T("Unknown logged user."),  _T("Error"), MB_OK);
		return false;
	}
	if(m_loggedUser != currentUser){
		SetControls();
		return true;
	}
	return false;
}

bool CObjMov::SetExpWeightOnMast(void){
	RETSTAT			retSt;
	float			objWeight;
	float			objtWeight;
	float			totFAGRWeight;
	float			totCLGRWeight;
	int				i;
	
	totFAGRWeight = 0.0;
	totCLGRWeight = 0.0;

	for(i = 0; i < N_MAX_NONAM_OBJ; i++){
		switch(i){
		objWeight = 0.0;
		objtWeight = 0.0;
		if(this->objOnMast.obj[i] != 0){
			case OBJT_N_FA:
				retSt = GetObjWeight(this->objOnMast.o_id[i], &objWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objWeight;
				break;
			case OBJT_N_CLUS:
				retSt = GetObjWeight(this->objOnMast.o_id[i], &objWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				if(this->objOnMast.obj[OBJT_N_CLUS_CAS] != 0)
					totCLGRWeight += objWeight;
				else
					totFAGRWeight += objWeight;
				break;
			case OBJT_N_HECA:
				retSt = GetObjWeight(this->objOnMast.o_id[i], &objWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objWeight;
				break;
			case OBJT_N_COVER:
				retSt = GetObjWeight(this->objOnMast.o_id[i], &objWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objWeight;
				break;
			case OBJT_N_CLUS_CAS:
				retSt = GetObjtWeight(this->objOnMast.obj[i], &objtWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object type weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objtWeight;
				break;
			case OBJT_N_CLUS_FRM:
				retSt = GetObjWeight(this->objOnMast.o_id[i], &objtWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object type weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objtWeight;
				break;
			case OBJT_N_FFDS_COV:
				retSt = GetObjtWeight(this->objOnMast.obj[i], &objtWeight);
				if(retSt != RT_NORMAL){
					MessageBox(_T("Error retrieving object type weight."),  _T("Error"), MB_ICONERROR | MB_OK);
					return false;
				}
				totFAGRWeight += objtWeight;
				break;
			}
		}
	}

	switch(this->m_cmbMast.GetCurSel()){
	case MAIN_MAST:
		retSt = VotedTagWrite(TWN_MM_EXP_WEIGHT, 0, (float)(totFAGRWeight), FLT_VALUE);
		if(retSt != RT_NORMAL)
			return(false);

		retSt = VotedTagWrite(TWN_MM_CL_EXP_WEIGHT, 0, (float)(totCLGRWeight), FLT_VALUE);
		if(retSt != RT_NORMAL)
			return(false);
		break;
	}
	return(true);	
}


bool CObjMov::SetObjectOnMast(GENIDX mastType, OBJTMM mastSituation){
	RETSTAT			retSt;
	int				i;
	int				objPresent;

	objPresent = 0;

	for(i = 0; i <= N_MAX_NONAM_OBJ; i++){
		if(mastSituation.obj[i] != 0)
			objPresent = 1;
		else
			objPresent = 0;
		switch(i){
		case OBJT_N_FA:
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_FA_ON_MM, objPresent, 0, DIG_VALUE);
				if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		case OBJT_N_CLUS:
			if(mastSituation.o_id[OBJT_N_CLUS_CAS] != 0){
				switch(mastType){
				case MAIN_MAST:
					retSt = VotedTagWrite(TWN_CLUS_ON_MM, objPresent, 0, DIG_VALUE);
					if(retSt != RT_NORMAL){
						return(false);
					}
					break;
				}
			}
			break;
		case OBJT_N_HECA:
			//Mancano le tag per NFR e SFR//
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_HECA_ON_MM, objPresent, 0, DIG_VALUE);
					if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		case OBJT_N_COVER:
			//Mancano le tag NFR e SFR//
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_HECA_PLUG_ON_MM, objPresent, 0, DIG_VALUE);
				if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		case OBJT_N_CLUS_CAS:
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_CLUSCAS_ON_MM, objPresent, 0, DIG_VALUE);
				if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		case OBJT_N_FFDS_COV:
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_FFDS_PLUG_ON_MM, objPresent, 0, DIG_VALUE);
				if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		case OBJT_N_CLUS_FRM:
			switch(mastType){
			case MAIN_MAST:
				retSt = VotedTagWrite(TWN_CLUSFRM_ON_MM, objPresent, 0, DIG_VALUE);
				if(retSt != RT_NORMAL){
					return(false);
				}
				break;
			}
			break;
		}
	}
	return(true);
}

void CObjMov::OnRefresh() 
{
	RETCOND		retCnd;
	RETSTAT		retSt;
	float		px, py;
	short int	vx, vy;
	ZONENAME	zoneName;
	GENIDX		zoneIdx;
	GENIDX		mastType;
	TAGIDX		tagIdx;
	TAGVALEX	tagVal;

	mastType = this->m_cmbMast.GetCurSel();

	retSt = GetMastPosInfiSC(mastType, &px, &py, &zoneIdx);
	if(retSt == RT_FAILED){
		MessageBox(_T("Cannot retrieve current mast position"),  _T("Error"), MB_OK | MB_ICONERROR);
		return;
	}
	else if(retSt == RT_INVZONE)
		goto NOT_ON_CELL;
	
	if(zoneIdx > 0){
		retSt = GetVirtCoordFromZoneAndPhysCoord(px, py, zoneIdx, &vx, &vy);
		if((retSt == RT_NORMAL)){
			this->m_MastOnCell = true;

			this->m_MastVirt.Format(_T("(%d,%d)"), vx, vy);

			retSt = GetZoneName(zoneIdx, zoneName);

			this->m_MastZone.Format(_T("%s"), zoneName);
		}
		else
			goto NOT_ON_CELL;
	}
	else{
NOT_ON_CELL:
		this->m_MastOnCell = false;
		this->m_MastVirt.Format(_T("Not on cell"));
		if(zoneIdx == -1)
			this->m_MastZone.Format(_T("C2-4"));
		else if(zoneIdx == -2)
			this->m_MastZone.Format(_T("C2-3"));
		else
			this->m_MastZone.Format(_T("Out of zones"));

	}

	this->m_MastPhys.Format(_T("(%.1f,%.1f)"), px, py);

	switch(mastType){
	case MAIN_MAST:
		retCnd = GetTagIdx(TWN_FAGR_POS, &tagIdx);
		if(retCnd != R_NORMAL)
			MessageBox(_T("Cannot retrieve Main Mast FA Gripper position tag Idx."),  _T("Error"), MB_OK | MB_ICONERROR);
		else{
			retCnd = TagRead(&tagIdx, &tagVal);
			if(retCnd != R_NORMAL){
				MessageBox(_T("Cannot retrieve Main Mast FA Gripper position.."),  _T("Error"), MB_OK | MB_ICONERROR);
				this->m_FaGrPos.Format(_T("Not retrieved"));
			}
			else
				this->m_FaGrPos.Format(_T("%.1f [mm]"), tagVal.value.val_float);
		}

		retCnd = GetTagIdx(TWN_CLGR_POS, &tagIdx);
		if(retCnd != R_NORMAL)
			MessageBox(_T("Cannot retrieve Main Mast Cluster Gripper position tag Idx."),  _T("Error"), MB_OK | MB_ICONERROR);
		else{
			retCnd = TagRead(&tagIdx, &tagVal);
			if(retCnd != R_NORMAL){
				MessageBox(_T("Cannot retrieve Main Mast Cluster Gripper position."),  _T("Error"), MB_OK | MB_ICONERROR);
				this->m_ClGrPos.Format(_T("Not retrieved"));
			}
			else
				this->m_ClGrPos.Format(_T("%.1f [mm]"), tagVal.value.val_float);
		}

		retCnd = GetTagIdx(TWN_MM_ROT_POS, &tagIdx);
		if(retCnd != R_NORMAL)
			MessageBox(_T("Cannot retrieve Main Mast Angle tag Idx."),  _T("Error"), MB_OK | MB_ICONERROR);
		else{
			retCnd = TagRead(&tagIdx, &tagVal);
			if(retCnd != R_NORMAL){
				MessageBox(_T("Cannot retrieve Main Mast Angle."),  _T("Error"), MB_OK | MB_ICONERROR);
				this->m_MastAngle.Format(_T("Not retrieved"));
			}
			else
				this->m_MastAngle.Format(_T("%.1f [Deg]"), tagVal.value.val_float);
		}
	
		
		retCnd = GetTagIdx(TWN_FAGRL_UNFIXED, &tagIdx);
		if(retCnd != R_NORMAL)
			MessageBox(_T("Cannot retrieve Main Mast FA Gripper Latch tag Idx."),  _T("Error"), MB_OK | MB_ICONERROR);
		else{
			retCnd = TagRead(&tagIdx, &tagVal);
			if(retCnd != R_NORMAL){
				MessageBox(_T("Cannot retrieve Main Mast FA Gripper Latch status."),  _T("Error"), MB_OK | MB_ICONERROR);
				this->m_FaGrL.Format(_T("Not retrieved"));
			}
			else{
				if(GtBit((short int)(tagVal.value.val_di), 15) == 1)
					this->m_FaGrL.Format(_T("OPEN"));
				else
					this->m_FaGrL.Format(_T("CLOSED"));
			}
		}
		break;
	default:
		break;
	}
		
	UpdateData(false);
}
